package cc.rengu.igas.share.biz.service.impl;

import cc.rengu.igas.share.common.enums.RespCodeEnum;
import cc.rengu.igas.share.facade.AlwxUnifiedService;
import cc.rengu.igas.share.facade.request.AlipayPayResultNoticeRequest;
import cc.rengu.igas.share.facade.request.WechatPayResultNoticeRequest;
import cc.rengu.igas.share.facade.response.AlipayPayResultNoticeResponse;
import cc.rengu.igas.share.facade.response.WechatPayResultNoticeResponse;
import cc.rengu.igas.share.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * Created by wangcheng on 2021/9/30 10:58
 */
public class AlwxUnifiedServiceImpl implements AlwxUnifiedService {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<WechatPayResultNoticeResponse> wechatPayResultNotice(WechatPayResultNoticeRequest request) throws Exception {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatPayResultNoticeResponse> result = new Result<>();
        WechatPayResultNoticeResponse payResultNoticeResponse = new WechatPayResultNoticeResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, WechatPayResultNoticeResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((WechatPayResultNoticeResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银联微信支付通知处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                payResultNoticeResponse.setRespCode(((BizException) e).getCode());
                payResultNoticeResponse.setRespDesc(e.getMessage());
            } else {
                payResultNoticeResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                payResultNoticeResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(payResultNoticeResponse);
        }
        return result;
    }

    @Override
    public Result<AlipayPayResultNoticeResponse> alipayPayResultNotice(AlipayPayResultNoticeRequest request) throws Exception {
        DubboService dubboService = new DubboServiceImpl();
        Result<AlipayPayResultNoticeResponse> result = new Result<>();
        AlipayPayResultNoticeResponse payResultNoticeResponse = new AlipayPayResultNoticeResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, AlipayPayResultNoticeResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AlipayPayResultNoticeResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银联支付宝支付通知处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                payResultNoticeResponse.setRespCode(((BizException) e).getCode());
                payResultNoticeResponse.setRespDesc(e.getMessage());
            } else {
                payResultNoticeResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                payResultNoticeResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(payResultNoticeResponse);
        }
        return result;
    }
}
